//
//  DESCrypt.m
//  Gurpartap Singh
//
//  Created by Gurpartap Singh on 06/05/12.
//  Copyright (c) 2012 Gurpartap Singh. All rights reserved.
//

#import "DESCrypt.h"

#import "NSData+Base64.h"
#import "NSString+Base64.h"

const Byte iv[] = {1,2,3,4,5,6,7,8};

@implementation DESCrypt

#pragma mark - Base64编码

/**
 *  加密 Base64
 *
 *  @param plainText  原文
 *  @param encryptKey 密钥
 *
 *  @return
 */
+ (NSString *)encrypt:(NSString *)plainText encryptKey:(NSString *)encryptKey
{
    NSString *ciphertext = nil;
    NSData *textData = [plainText dataUsingEncoding:NSUTF8StringEncoding];
    NSUInteger dataLength = [textData length];
    unsigned char buffer[1024];
    memset(buffer, 0, sizeof(char));
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding,
                                          [encryptKey UTF8String], kCCKeySizeDES,
                                          iv,
                                          [textData bytes], dataLength,
                                          buffer, 1024,
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        
        NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
        ciphertext = [NSString base64StringFromData:data length:[data length]];
    }
    
    return ciphertext;
}

/**
 *  解密 Base64
 *
 *  @param cipherText 密文
 *  @param encryptKey 密钥
 *
 *  @return
 */
+ (NSString *)decrypt:(NSString *)cipherText encryptKey:(NSString *)encryptKey
{
    NSString *plaintext = nil;
    NSData *cipherdata = [NSData base64DataFromString:cipherText];
    unsigned char buffer[1024];
    memset(buffer, 0, sizeof(char));
    size_t numBytesDecrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding,
                                          [encryptKey UTF8String], kCCKeySizeDES,
                                          iv,
                                          [cipherdata bytes], [cipherdata length],
                                          buffer, 1024,
                                          &numBytesDecrypted);
    if(cryptStatus == kCCSuccess) {
        NSData *plaindata = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesDecrypted];
        plaintext = [[NSString alloc]initWithData:plaindata encoding:NSUTF8StringEncoding];
    }
    return plaintext;
}


#pragma mark - 16进制Hex

/**
 *  加密 16进制
 *
 *  @param plainText  原文
 *  @param encryptKey 密钥
 *
 *  @return
 */
+ (NSString *)encrypt2:(NSString *)plainText encryptKey:(NSString *)encryptKey
{
    NSString *ciphertext = nil;
    NSData *textData = [plainText dataUsingEncoding:NSUTF8StringEncoding];
    NSUInteger dataLength = [textData length];
    unsigned char buffer[1024];
    memset(buffer, 0, sizeof(char));
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding,
                                          [encryptKey UTF8String], kCCKeySizeDES,
                                          [encryptKey UTF8String],
                                          [textData bytes], dataLength,
                                          buffer, 1024,
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
        ciphertext = [self parseDataToHexString:data];
    }
    
    return ciphertext;
}

/**
 *  解密 16进制
 *
 *  @param cipherText  密文
 *  @param encryptKey  密钥
 *
 *  @return
 */
+ (NSString *)decrypt2:(NSString *)cipherText encryptKey:(NSString *)encryptKey
{
    NSString *plaintext = nil;
    NSData *cipherdata = [self parseHexToByteArray:cipherText];
    unsigned char buffer[1024];
    memset(buffer, 0, sizeof(char));
    size_t numBytesDecrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding,
                                          [encryptKey UTF8String], kCCKeySizeDES,
                                          [encryptKey UTF8String],
                                          [cipherdata bytes], [cipherdata length],
                                          buffer, 1024,
                                          &numBytesDecrypted);
    if(cryptStatus == kCCSuccess) {
        NSData *plaindata = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesDecrypted];
        plaintext = [[NSString alloc] initWithData:plaindata encoding:NSUTF8StringEncoding];
    }
    
    return plaintext;
}


#pragma mark - 16进制Hex

/**
 *  将NSData数组转化成16进制数据
 *
 *  @param data
 *
 *  @return
 */
+ (NSString *)parseDataToHexString:(NSData *)data
{
    Byte *bytes = (Byte *)[data bytes];
    NSMutableString *hexStr = [[NSMutableString alloc]init];
    if (bytes) {
        for (int i = 0; i < data.length; i++) {
            NSString *hexByte = [NSString stringWithFormat:@"%x", bytes[i] & 0xff];
            if([hexByte length] == 1) {
                [hexStr appendFormat:@"0%@", hexByte];
            } else {
                [hexStr appendFormat:@"%@", hexByte];
            }
        }
    }
    
    return [hexStr uppercaseString];
}

/**
 *  将16进制数据转化成NSData数组
 *
 *  @param hexString
 *
 *  @return
 */
+ (NSData *)parseHexToByteArray:(NSString *)hexString
{
    int j = 0;
    Byte bytes[hexString.length];
    for (int i = 0; i < [hexString length]; i++) {
        int int_ch;
        unichar hex_char1 = [hexString characterAtIndex:i];
        int int_ch1;
        
        if(hex_char1 >= '0' && hex_char1 <= '9') {
            int_ch1 = (hex_char1 - 48) *16;
        } else if (hex_char1 >= 'A' && hex_char1 <= 'F') {
            int_ch1 = (hex_char1 - 55) * 16;
        } else {
            int_ch1 = (hex_char1 - 87) * 16;
        }
        
        i++;
        
        unichar hex_char2 = [hexString characterAtIndex:i];
        int int_ch2;
        
        if(hex_char2 >= '0' && hex_char2 <= '9') {
            int_ch2 = (hex_char2 - 48);
        } else if (hex_char2 >= 'A' && hex_char1 <= 'F') {
            int_ch2 = hex_char2 - 55;
        } else {
            int_ch2 = hex_char2 - 87;
        }
        
        int_ch = int_ch1 + int_ch2;
        bytes[j] = int_ch;
        j++;
    }
    
    NSData *newData = [[NSData alloc] initWithBytes:bytes length:hexString.length / 2];
    return newData;
}

@end
